package com.bytedance.scenedemo.migrate;

import com.bytedance.scene.LifeCycleFrameLayout;
import com.bytedance.scene.Scene;
import com.bytedance.scene.SceneComponentFactory;
import com.bytedance.scene.Scope;
import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scene.navigation.NavigationSceneOptions;
import com.bytedance.scene.utlity.SceneInstanceUtility;
import com.bytedance.scenedemo.lifecycle.EmptyScene;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * Created by JiangQi on 11/6/18.
 */
public class TestSceneDelegateToViewView extends LifeCycleFrameLayout {
    public static final String SERVICE_NAME = "SERVICE";

    public TestSceneDelegateToViewView(Context context) {
        super(context);
        init();
    }

    public TestSceneDelegateToViewView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    public TestSceneDelegateToViewView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        System.out.println("=========================TestSceneDelegateToViewView");
        NavigationSceneOptions options = new NavigationSceneOptions(EmptyScene.class);
        options.setDrawWindowBackground(false);
        NavigationScene navigationScene = (NavigationScene) SceneInstanceUtility.getInstanceFromClass(NavigationScene.class,
                options.toBundle());
        setNavigationScene(navigationScene);
        setRootSceneComponentFactory(new SceneComponentFactory() {
            @Override
            public Scene instantiateScene(ClassLoader cl, String className, IntentParams bundle) {
                showToast("Create " + className);
                return null;
            }
        });
        setRootScopeFactory(new Scope.RootScopeFactory() {
            @Override
            public Scope getRootScope() {
                Scope scope = Scope.DEFAULT_ROOT_SCOPE_FACTORY.getRootScope();
                scope.register(TestSceneDelegateToViewView.SERVICE_NAME, "Test");
                return scope;
            }
        });
    }

    @Override
    public boolean isSupportRestore() {
        return true;
    }

    public static class TestScene extends Scene {
        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            Component component = new Component(getAbility());
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFF0000));
            component.setBackground(shapeElement);
            return null;
        }

        @Override
        public void onAbilityCreated(Intent intent) {
            super.onAbilityCreated(intent);
            String value = getScope().getService(TestSceneDelegateToViewView.SERVICE_NAME);
            showToast(value);
        }
    }

    /**
     * Toast弹框
     *
     * @param content 内容
     */
    private void showToast(String content) {
        Component toastLayout = LayoutScatter.getInstance(getContext())
                .parse(com.bytedance.scene.ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(com.bytedance.scene.ResourceTable.Id_text_msg_toast);
        toastText.setText(content);
        new ToastDialog(getContext())
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }

}
